<?php

namespace App\Http\Controllers\system;

use App\Http\Controllers\Controller;
use App\Models\system\ConfigModel;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class ConfigureController extends Controller
{

    /**
     * 获取常规配置页面数据
     */
    public function getConventionData()
    {
        $data['system'] = Tools::config('system');
        $data['introduce'] = Tools::config('introduce');

        $data['introduce']['xcx_lawyer_content_data'] = [];
        $data['introduce']['xcx_user_content_data'] = [];

        if ($data['introduce']['xcx_lawyer_content']) {
            $data['introduce']['xcx_lawyer_content'] = explode(',', $data['introduce']['xcx_lawyer_content']);
            foreach ($data['introduce']['xcx_lawyer_content'] as $k => $v) {
                $data['introduce']['xcx_lawyer_content_data'][$k]['url'] = $v;
            }
        } else {
            $data['introduce']['xcx_lawyer_content'] = [];
        }

        if ($data['introduce']['xcx_user_content']) {
            $data['introduce']['xcx_user_content'] = explode(',', $data['introduce']['xcx_user_content']);
            foreach ($data['introduce']['xcx_user_content'] as $k => $v) {
                $data['introduce']['xcx_user_content_data'][$k]['url'] = $v;
            }
        } else {
            $data['introduce']['xcx_user_content'] = [];
        }

        return ApiResponse::success('获取常规配置页面数据成功', $data);
    }


    /**
     * 更新常规配置
     */
    public function conventionFormSubmit()
    {
        $post['system']['CURSTOMER_QRCODE'] = request()->input('system_CURSTOMER_QRCODE');
        $post['system']['WECHAT_LOGO'] = request()->input('system_WECHAT_LOGO');
        $post['introduce']['xcx_lawyer_content'] = request()->input('introduce_xcx_lawyer_content');
        $post['introduce']['xcx_user_content'] = request()->input('introduce_xcx_user_content');
        $post['introduce']['cover_1'] = request()->input('introduce_cover_1');
        $post['introduce']['cover_2'] = request()->input('introduce_cover_2');
        $post['introduce']['cover_3'] = request()->input('introduce_cover_3');

        if ($post['introduce']['xcx_lawyer_content']) {
            $post['introduce']['xcx_lawyer_content'] = implode(',', $post['introduce']['xcx_lawyer_content']);
        }
        if ($post['introduce']['xcx_user_content']) {
            $post['introduce']['xcx_user_content'] = implode(',', $post['introduce']['xcx_user_content']);
        }

        foreach ($post as $k => $v) {
            foreach ($v as $kk => $vv) {
                $map = ['type' => $k, 'name' => $kk];
                $data = ['value' => $vv];
                $query = ConfigModel::where($map);
                if (count($query->get()) > 0) {
                    $query->update($data);
                } else {
                    $query->create($data);
                }
            }
        }

        return ApiResponse::success('更新常规配置成功');
    }


    /**
     * 获取用户配置页面数据
     */
    public function getUserData()
    {
        $data = Tools::config('user');

        if ($data['CANCEL_ACCOUNT']) {
            $data['CANCEL_ACCOUNT'] = explode(',', $data['CANCEL_ACCOUNT']);
            foreach ($data['CANCEL_ACCOUNT'] as $k => $v) {
                $data['CANCEL_ACCOUNT_DATA'][$k]['url'] = $v;
            }
        } else {
            $data['CANCEL_ACCOUNT'] = [];
        }

        return ApiResponse::success('获取用户配置页面数据成功', $data);
    }


    /**
     * 更新用户配置
     */
    public function userFormSubmit()
    {
        $post = request()->input();
        if ($post['CANCEL_ACCOUNT']) {
            $post['CANCEL_ACCOUNT'] = implode(',', $post['CANCEL_ACCOUNT']);
        }

        foreach ($post as $k => $v) {
            $map = ['type' => 'user', 'name' => $k];
            $data = ['value' => $v];
            $query = ConfigModel::where($map);
            if (count($query->get()) > 0) {
                $query->update($data);
            } else {
                $query->create($data);
            }
        }

        return ApiResponse::success('更新用户配置成功');
    }

}
